// user信息仓库
import { getUserInfoAPI } from '@/apis/user'
import type { UserInfoRes } from '@/types'
import { getLocalToken, removeLocalToken, setLocalToken } from '@/utils/auth'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('userInfo', () => {
  const userInfo = ref<UserInfoRes | null>(null)

  const getUserInfo = async () => {
    const res = await getUserInfoAPI()
    userInfo.value = res.data
  }
  const removeUserInfo = () => {
    userInfo.value = null
  }
  // token部分
  // 获取token
  const token = ref(getLocalToken() || '')
  // 设置token
  const setToken = (val: string) => {
    token.value = val
    // 设置到cookie中
    setLocalToken(val)
  }
  // 移除token
  const removeToken = () => {
    token.value = ''
    removeLocalToken()
  }

  return {
    token,
    setToken,
    removeToken,
    userInfo,
    getUserInfo,
    removeUserInfo,
  }
})
